w is 25.0 x is 1.0
Our goal is to write a program that computes the following sum:
sum = 1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6
(This may look like a pointless thing to do, but you will see such sums in calculus, where they are called harmonic series.) Here is a skeleton of the program:
// Class definition for HarmonicSeries
class HarmonicSeries
{
double value()
{
int term=1, lastTerm = 6;
double sum = 0.0;
while ( term <= lastTerm )
{
____________________ // add the next term to sum
____________________ // increment term
}
return sum;
}
}
// Class for testing HarmonicSeries objects
class HarmonicTester
{
public static void main ( String[] args )
{
HarmonicSeries series = new HarmonicSeries();
System.out.println("Sum of 6 terms:" + series.value() ) ;
}
}
A few notes on the program:
HarmonicSeries describes an object that can compute the sum we want.value() of that object will do the computation.HarmonicSeries constructor exists automatically, even though the
program doesn't explicitly describe one.HarmonicTester creates a HarmonicSeries object, then
uses value() to calculate the sum, then prints it out.